<%@ page import="java.io.FileWriter" %>
<%--
  Created by IntelliJ IDEA.
  User: Specter
  Date: 2019/5/5
  Time: 13:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="User.Userjson" %>
<%@ page import="User.UserInfo" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人中心</title>
    <style>
        body{
            background: url("/css/bg.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            overflow-x: hidden;
        }
        .mess{
            width: 100%;
            height: 30px;
            float: top;
        }
        .mess1{
            width: 250px;
            height: 600px;
            background-color: #ded7d7;
            float: left;
            opacity: 0.85;
            margin-left: -75px;
        }
        .tx{
            width: 100px;
            height: 100px;
            border-radius: 50%;

        }
        .mess2{
            width: 75%;
            height:600px;
            background-color: #ded7d7;
            float: right;
            opacity: 0.85;
            margin-right: 50px;
        }
        .hrLine{
            vertical-align:middle;
            display:inline-block;
        }
    </style>
</head>
<body>
<%
    int flag=0;
    String a=null;
    String surname=null,username=null,mail=null,name=null,xueyuan=null,group=null,dgut=null;
    boolean bind=false;
    String Path=null;
    UserInfo bean=new UserInfo();
    Userjson bean1=new Userjson();
    String path=request.getContextPath();
    if(session.getAttribute("UserInfo")==null&&session.getAttribute("denglu")==null)//如果既没有进行莞工登录也没有进行本地登录，就返回登录界面
    {
        response.sendRedirect("http://localhost:8080/Login.jsp");
    }
    else if(session.getAttribute("UserInfo")!=null&&session.getAttribute("denglu")==null)//只进行了本地登录
    {
        bean= (UserInfo)session.getAttribute("UserInfo");
        username=bean.getUsername();
        surname=bean.getSurname();
        name=bean.getName();
        mail=bean.getMail();
        a="Login.jsp";
        if(session.getAttribute("path")==null)Path=bean.GetTX(username);
        else{
            Path="css/"+(String) session.getAttribute("path");
            if(!Path.equals("css/a.jpg"))bean.SetTX(username,Path);
        }
        if(session.getAttribute("bebinded")!=null){
            String bebind=(String)session.getAttribute("bebinded");
            %>
                <script>alert("<%=bebind%>")</script>
            <%
        }
        String b=bean.CheckBind2(bean.getUsername());//检查此本地账号是否已经绑定了莞工账号
        if(session.getAttribute("bind")==null)//之前就已经绑定或没有绑定莞工账号的本地账号
        {
            if(b==null)//没有绑定莞工账号
            {
                xueyuan = null;
                group = null;
                dgut = null;
            }
            else //已经绑定莞工账号
            {
                try {
                    InitialContext context = new InitialContext();
                    DataSource dataSource = (DataSource) context.lookup("jpa/DSTest");
                    Connection conn = null;
                    Statement stmt = null;
                    conn = dataSource.getConnection();
                    stmt = conn.createStatement();
                    ResultSet rs=stmt.executeQuery("select * from DGUT where `username`='"+b+"';");
                    if(rs.next()) {
                        surname=rs.getString("surname");
                        name=rs.getString("name");
                        xueyuan = rs.getString("faculty_title");
                        group = rs.getString("group");
                        dgut = rs.getString("username");
                        bind=true;
                    }
                    stmt.close();
                    conn.close();
                    context.close();
                }catch (Exception e){
                    e.printStackTrace();
                    response.sendRedirect("error.jsp");
                }
            }
            a="Login.jsp";
        }
        else//刚刚才进行绑定莞工账号的本地账号
        {
            Userjson json=(Userjson) session.getAttribute("bind");
            surname = json.getName().substring(0, 1);
            name = json.getName().substring(1, json.getName().length());
            xueyuan=json.getFaculty_title();
            group=json.getGroup();
            dgut=json.getUsername();
            bind=true;
            a="Login.jsp";
        }
    }
    else //只进行了莞工登录
    {
        flag=1;
        bean1=(Userjson)session.getAttribute("denglu");
        username=bean1.getUsername();
        surname=bean1.getName().substring(0, 1);
        name=bean1.getName().substring(1, bean1.getName().length());
        xueyuan=bean1.getFaculty_title();
        group=bean1.getGroup();
        dgut=bean1.getUsername();
        a="/login/dgut";
        bind = true;
        if(session.getAttribute("path")!=null) {
            Path = "css/" + (String) session.getAttribute("path");
            if (Path.equals("css/null")) Path = "css/a.jpg";
        }
        if(bean.CheckBind1(username))//已经绑定了本地账号的莞工账号
        {
            try {
                InitialContext context = new InitialContext();
                DataSource dataSource = (DataSource) context.lookup("jpa/DSTest");
                Connection conn = null;
                Statement stmt = null;
                conn = dataSource.getConnection();
                stmt = conn.createStatement();
                ResultSet rs=stmt.executeQuery("select * from LOCAL where `dgut_username`='"+username+"';");
                if(rs.next()) {
                    username=rs.getString("username");
                    mail = rs.getString("mail");
                }
                stmt.close();
                conn.close();
                context.close();
            }catch (Exception e){
                e.printStackTrace();
                response.sendRedirect("error.jsp");
            }
            if(session.getAttribute("path")==null)Path=bean.GetTX(username);
            else{
                Path="css/"+(String) session.getAttribute("path");
                if(!Path.equals("css/a.jpg"))bean.SetTX(username,Path);
            }
        }
        else//没有绑定本地账号的莞工账号
        {
            mail=null;
            Path="css/a.jpg";
        }
    }
%>
<div class="mess">
    <h3 style="color: white;float: left;margin-top: 0px;margin-left: 25px;">绿色生活</h3>
    <form  action="<%=a%>" method="post">
        <input type="submit" value="退出" style="float: right;">
    </form>
</div>
<div class="mess1">
    <form action="/upload" method="post" enctype="multipart/form-data">
    <div style="text-align:center;">
        <div style="float: left;">
            <input type="submit" value="上传头像" style="color:blue;border: none;background-color: #ded7d7;" >
        </div>
        <br>
        <div>
        <a href="" title="更换头像">
            <label for="file">
                <img src="<%=Path%>" alt="" class="tx">
            </label>
            <input style="opacity:0;width:0px;color: #ded7d7;" required="required" type="file" id="file" name="file" accept="image/*"><br>
        </a>
        </div>
    </div>
    </form>
    <h6 style="text-align:center;margin-top: 0px;margin-bottom: 0px;color:lightcyan ;">
        (点击头像选择要更换的头像)
    </h6>
    <h4 style="text-align:center;margin-top: 0px;margin-bottom: 0px;">
        <%=surname%>先生
    </h4>
    <hr>
    <h3 style="text-align: center;">个人信息</h3>
</div>
<div class="mess2">
    <h2 style="text-align: center;margin-bottom: 0px;">账户设置</h2>
    <h3 style="text-align: center;margin-top: 0px;color:#9a9696; ">管理你的账户信息并设置邮箱</h3>
    <div style=" width:100%; text-align:left; margin-left:15px; margin-right:15px;">
        <hr class="hrLine" style="width:50px;"/>个人信息<hr class="hrLine" style="width:80%;"/>
    </div>
    <form action="/Binding" method="post">
        <div style="float: left;margin-left:50px; ">
            <br>
            <div style="margin-bottom:0px;">用户名</div>
            <h4 style="margin-top:0px;"><%=username%></h4>
            <div style="margin-bottom:8px;margin-top:14px;">姓</div>
            <h4 style="margin-top:0px;"><%=surname%></h4>
            <div style="margin-bottom:8px;margin-top:14px;">学院</div>
            <h4 style="margin-top:0px;"><%=xueyuan%></h4>
            <div style="margin-bottom:8px;margin-top:14px;">类型</div>
            <h4 style="margin-top:0px;" ><%=group%></h4>
        </div>
        <div style="float: right;margin-right:300px; ">
            <br>
            <div style="margin-bottom:8px;">邮箱</div>
            <h4 style="margin-top:0px;"><%=mail%></h4>
            <div style="margin-bottom:8px;margin-top:14px;">名</div>
            <h4 style="margin-top:0px;" ><%=name%></h4>
            <%
                if(flag==0){
            %>
            <div style="margin-bottom:8px;margin-top:14px;">绑定的莞工账号</div>
            <h4 style="margin-top:0px;" ><%=dgut%></h4>
            <%
                }
            %>
            <%
                if(!bind){
            %><a href="/login/dgut" style="color: #128cb3;margin-bottom: 0px;">绑定莞工账号</a><%
            }
            %>
        </div>

    </form>
</div>
<script></script>
</body>
</html>
